/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.generic_return_type;

import java.util.HashMap;
import java.util.Map;

class Casts {
	private final Map<Integer, Cast> castsByLineNumber = new HashMap<Integer, Cast>();

	public void addCastByLineNumber(int line, String castTo) {
		if (haveCastsForLine(line) && !castTo.equals(getCastForLine(line))) {
			System.out.println("Cast to: " + castTo + ", overriding cast to: " +
				getCastForLine(line) + " on line: " + line);
		}

		castsByLineNumber.put(line, new RealCast(castTo));
	}

	public Cast getCastForLine(int line) {
		return castsByLineNumber.containsKey(line) ? castsByLineNumber.get(line) : NullCast.INSTANCE;
	}
	
	@Override
	public String toString() {
		return castsByLineNumber.toString();
	}

	private boolean haveCastsForLine(int line) {
		return castsByLineNumber.containsKey(line);
	}

	public static final Casts NULL = new Casts() {
		@Override
		public Cast getCastForLine(int line) {
			return NullCast.INSTANCE;
		}
	
		@Override
		public void addCastByLineNumber(int line, String castTo) {
		}
	};
}